package com.sphd.emanu.fragment.materialslist;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sphd.emanu.R;
import com.sphd.emanu.activity.materials.MaterialsActivity;
import com.sphd.emanu.adapter.MaterialAdapter;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.interfaces.Letter;
import com.sphd.emanu.mvp.MVPBaseFragment;
import com.sphd.emanu.permission.Moudle;
import com.sphd.emanu.pojo.goods.Category;
import com.sphd.emanu.pojo.login.User;
import com.sphd.emanu.pojo.material.Material;
import com.sphd.emanu.utils.PermissionConfig;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class MaterialsListFragment extends MVPBaseFragment<MaterialsListContract.View, MaterialsListPresenter> implements MaterialsListContract.View {

    private List<Material> data;

    @BindView(R.id.my_recycler)
    RecyclerView myRecycler;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    Unbinder unbinder;
    MaterialAdapter materialAdapter;
    Bundle bundle;
    Category category;
    Intent intent;
    User user;
    List<Moudle> moudles;
    boolean isCanDo;
    Letter letter;

    public void setLetter(Letter letter) {
        this.letter = letter;
    }

    public void setData(List<Material> data) {
        this.data = data;
        materialAdapter.setNewData(data);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.myswf_and_recycler;
    }

    @Override
    protected void initView() {
        intent=new Intent();
        unbinder = ButterKnife.bind(this, rootView);
        initEvent();
        bundle = getArguments();
        data = (List<Material>) bundle.getSerializable(Constant.MATERIAL_LIST);
        category= (Category) bundle.getSerializable(Constant.CATEGORY);
        user = (User) bundle.getSerializable(Constant.USER_ENTITY);
        moudles = (List<Moudle>) bundle.getSerializable(Constant.PERMISSION_LIST);
        isCanDo = isCanDo(moudles, PermissionConfig.MENU_STOREHOUSE_8);
        initAdapter();
        materialAdapter.setNewData(data);
    }

    private void initEvent() {
        mySwf.setOnRefreshListener(() -> mySwf.setRefreshing(false));
        myRecycler.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {

                Material material = (Material) adapter.getItem(position);
                bundle.putInt(Constant.FLAG, 1);
                bundle.putSerializable(Constant.MATERIAL, material);
                bundle.putSerializable(Constant.CATEGORY, category);
                intent.putExtras(bundle);
                intent.setClass(context, MaterialsActivity.class);
                startActivityForResult(intent, Constant.REQUEST_CODE);
            }

            @Override
            public void onItemLongClick(final BaseQuickAdapter adapter, View view, final int position) {
                super.onItemLongClick(adapter, view, position);
                if (isCanDo) {
                    new SweetAlertDialog(context, SweetAlertDialog.WARNING_TYPE).setTitleText("确定要删除吗？")
                            .setConfirmText("确定").setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                        @Override
                        public void onClick(SweetAlertDialog sweetAlertDialog) {
                            showProgressDialog();
                            mPresenter.deleteMaterial(((Material) adapter.getItem(position)).getId(), position, sweetAlertDialog);
                        }
                    }).setCancelText("取消").setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
                        @Override
                        public void onClick(SweetAlertDialog sweetAlertDialog) {
                            sweetAlertDialog.dismiss();
                        }
                    }).show();
                }


            }
        });
    }

    private void initAdapter() {
        setRefresh(mySwf);
        setRecy(myRecycler);
        materialAdapter = new MaterialAdapter(R.layout.item_material, new ArrayList<>());
        if (null != materialAdapter.getEmptyView()) {
            materialAdapter.setEmptyView(emptyView);
        }
        myRecycler.setAdapter(materialAdapter);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void setRes(int res, int pos, SweetAlertDialog sweetAlertDialog) {
        dissProgressDialog();
        if (res==1){
            toast("操作成功");
            materialAdapter.remove(pos);
            sweetAlertDialog.dismiss();
        }else {
            toast("操作失败，请重试！");

        }
    }

}
